<?php

namespace Home\Model;

use Think\Model;

class StoreModel extends Model {

    function get_info($store_id) {
        $info = $this->field(C('DB_PREFIX') .'store.*,user_name, email')->where('store_id=' . $store_id)->join('LEFT JOIN ' . C('DB_PREFIX') . 'member  ON ' . C('DB_PREFIX') . 'store.store_id = ' . C('DB_PREFIX') . 'member.user_id')->find();
        $info['invite_code'] = set_encrypt($store_id);
        if (!empty($info['certification'])) {
            $info['certifications'] = explode(',', $info['certification']);
        }
        return $info;
    }
    
    
    
    //更新店铺得分
    function update_recount($store_id){
        
        $order_goods_mod = M('order_goods');
        
        $info = $order_goods_mod
                ->field('SUM(evaluation_desc) AS evaluation_desc ,SUM(evaluation_service) AS evaluation_service , SUM(evaluation_speed) AS evaluation_speed , SUM(credit_value) AS credit_value , COUNT(*) as evaluation_count')
                ->alias('order_goods')
                ->where('evaluation_status=1 AND is_valid = 1 AND seller_id='.$store_id)
                ->join('LEFT JOIN ' . C('DB_PREFIX') .'order ord ON ord.order_id = order_goods.order_id')
                ->find();
        
        $data['evaluation_desc']    = round(($info['evaluation_desc']/$info['evaluation_count']),1);
        $data['evaluation_service'] = round(($info['evaluation_service']/$info['evaluation_count']),1);
        $data['evaluation_speed']   = round(($info['evaluation_speed']/$info['evaluation_count']),1);
        $data['credit_value'] = $info['credit_value'];
        
        //找出为好评的记录
        $info = $order_goods_mod
                ->field('COUNT(*) as praise_count')
                ->alias('order_goods')
                ->join('LEFT JOIN ' . C('DB_PREFIX') .'order ord ON ord.order_id = order_goods.order_id')
                ->where('evaluation_status=1 AND is_valid = 1 AND evaluation=3 AND  seller_id='.$store_id)
                ->find();
        $data['praise_rate'] = round(($info['praise_count'] / $info['evaluation_count']), 4) * 100;
        
        $this->where('store_id='.$store_id)->save($data);
    }
    
    /**
     *   重新计算 ， 发货速度 、商品描述 、 服务态度的总值
     */
    function recount_evaluation_dss($store_id)
    {
        $average_score = array();
        $model_ordergoods =& m('ordergoods');
        /* 找出所有is_valid为1的商品评价记录，计算他们的credit_value的和 */
        $info = $model_ordergoods->get(array(
            'join'          => 'belongs_to_order',
            'conditions'    => "seller_id={$store_id} AND evaluation_status=1 AND is_valid = 1",
            'fields'        => 'SUM(evaluation_desc) AS evaluation_desc ,SUM(evaluation_service) AS evaluation_service , SUM(evaluation_speed) AS evaluation_speed ,  COUNT(*) as evaluation_count',
        ));
        $average_score['evaluation_desc']    = round(($info['evaluation_desc']/$info['evaluation_count']),1);
        $average_score['evaluation_service'] = round(($info['evaluation_service']/$info['evaluation_count']),1);
        $average_score['evaluation_speed']   = round(($info['evaluation_speed']/$info['evaluation_count']),1);
        return $average_score;
    }
    
    /**
     *    重新计算信用度
     */
    function recount_credit_value($store_id)
    {
        $credit_value = 0;
        $model_ordergoods =& m('ordergoods');
        /* 找出所有is_valid为1的商品评价记录，计算他们的credit_value的和 */
        $info = $model_ordergoods->get(array(
            'join'          => 'belongs_to_order',
            'conditions'    => "seller_id={$store_id} AND evaluation_status=1 AND is_valid = 1",
            'fields'        => 'SUM(credit_value) AS credit_value',
            'index_key'     => false,   /* 不需要索引 */
        ));
        $credit_value = $info['credit_value'];

        return $credit_value;
    }
    /**
     *    重新计算好评率
     */
    function recount_praise_rate($store_id)
    {
        $praise_rate = 0.00;
        $model_ordergoods =& m('ordergoods');

        /* 找出所有is_valid为1的商品中的商品评价记录总数 */
        $info  = $model_ordergoods->get(array(
            'join'          => 'belongs_to_order',
            'conditions'    => "seller_id={$store_id} AND evaluation_status=1 AND is_valid=1",
            'fields'        => 'COUNT(*) as evaluation_count',
            'index_key'     => false,   /* 不需要索引 */
        ));
        $evaluation_count = $info['evaluation_count'];
        if (!$evaluation_count)
        {
            return $praise_count;
        }

        /* 找出所有的evaluation为3的记录总数 */
        $info = $model_ordergoods->get(array(
            'join'          => 'belongs_to_order',
            'conditions'    => "seller_id={$store_id} AND evaluation_status=1 AND is_valid=1 AND evaluation=3",
            'fields'        => 'COUNT(*) as praise_count',
            'index_key'     => false,   /* 不需要索引 */
        ));
        $praise_count = $info['praise_count'];
        /* 计算好评数占总数的百分比 */
        $praise_rate = round(($praise_count / $evaluation_count), 4) * 100;
        
        return $praise_rate;
    }
    
    
    
    

}

?>
